package com.zkthink.ceres.promotion.strategy;

import com.zkthink.ceres.promotion.dto.PromotionDetailDTO;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 阶梯优惠
 * </p>
 *
 * @author vivi.huang
 * @since 2020/6/23
 */
public class LadderDiscountStrategy implements PromotionPlanStrategy {

    /**
     * 阶梯优惠
     * 例子
     * 优惠方案：[满300减30， 满500减50]
     * 订单金额1500
     * 优惠算法： 1500 - 50
     *
     * @param orderAmount
     * @param promotionDetailDTOList
     * @return
     */
    @Override
    public Long calculateAmount(Long orderAmount, List<PromotionDetailDTO> promotionDetailDTOList) {
        Long result = orderAmount - getMaxPromotionDetail(orderAmount, promotionDetailDTOList).getDiscountAmount();
        return result;
    }

    /**
     * 遍历优惠方案，找出订单金额对比方案金额的最大值
     *
     * @param orderAmount
     * @param promotionDetailDTOList
     * @return
     */
    private PromotionDetailDTO getMaxPromotionDetail(Long orderAmount, List<PromotionDetailDTO> promotionDetailDTOList) {

        List<PromotionDetailDTO> promotionDetailDTOS = promotionDetailDTOList.stream()
                .filter(f -> orderAmount > f.getFullAmount())
                .sorted(Comparator.comparing(PromotionDetailDTO::getFullAmount))
                .collect(Collectors.toList());

        return promotionDetailDTOS.get(0);

    }
}